using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 净化Buff - 移除所有负面效���
/// </summary>
public class CleanseBuff : BuffBase
{
    private List<BuffBase> m_RemovedBuffs;

    public CleanseBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        m_RemovedBuffs = new List<BuffBase>();
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 移除所有负面效���
        RemoveNegativeBuffs();
        
        // 播放净化特���
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 被净化了，移除了 {m_RemovedBuffs.Count} 个负面效���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 净化是瞬间效果，不需要持续处���
    }

    private void RemoveNegativeBuffs()
    {
        if (Target.BuffComponent == null) return;
        
        // 获取所有活跃的Buff
        var activeBuffs = Target.BuffComponent.GetActiveBuffs();
        
        foreach (var buff in activeBuffs)
        {
            // 检查是否为负面效果
            if (IsNegativeBuff(buff.BuffData.BuffType))
            {
                m_RemovedBuffs.Add(buff);
                Target.BuffComponent.RemoveBuff(buff.BuffData.BuffType);
            }
        }
    }

    private bool IsNegativeBuff(BuffType buffType)
    {
        // 定义负面效果类型
        switch (buffType)
        {
            case BuffType.Stun:
            case BuffType.Freeze:
            case BuffType.Petrify:
            case BuffType.Snare:
            case BuffType.Silence:
            case BuffType.Disarm:
            case BuffType.Blind:
            case BuffType.Slow:
            case BuffType.Taunt:
            case BuffType.Charm:
            case BuffType.Fear:
            case BuffType.Confuse:
            case BuffType.Sleep:
            case BuffType.Bleed:
            case BuffType.Poison:
            case BuffType.Burn:
            case BuffType.Shock:
            case BuffType.Chill:
            case BuffType.Curse:
            case BuffType.Corrosion:
            case BuffType.Weakness:
            case BuffType.ArmorShred:
            case BuffType.Vulnerability:
            case BuffType.Sluggish:
                return true;
            default:
                return false;
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 净化效果结���");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Cleanse"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 净化效果不能叠���
    }
}
